{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e0ea0d0a",
   "metadata": {},
   "source": [
    "## * 日期：2022-10-12 \\week08\n",
    "* 课程：Python\n",
    "* 记录人：文佩"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fe5685a",
   "metadata": {},
   "source": [
    "# 本周知识点\n",
    "> 1.有序的数据结构——字符串\n",
    ">> A.字符串的基本概念（回顾）\n",
    ">>> a.Single quotes: 'allows embedded \"double\" quotes'   \n",
    ">>> b.Double quotes: \"allows embedded 'single' quotes\"    \n",
    ">>> c.Triple quoted: '''Three single quotes''', \"\"\"Three double quotes\"\"\"  \n",
    "\n",
    ">> B.字符串的性质和方法 \n",
    "\n",
    "> 2.有序的数据结构_列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "803a253f",
   "metadata": {},
   "source": [
    "## 1.字符串的性质和方法\n",
    "\n",
    "* 字符串的性质\n",
    "> 1.字符串的有序性   \n",
    "> 2.字符串是由长度的    \n",
    "\n",
    ">> 1.使用len（）检查字符串的长度\n",
    "\n",
    "* 字符串的基础方法(运算法则）\n",
    "> 1.字符串的取值：str[index]\n",
    "> 2.字符串的切片：str[start,stop],stop取不到\n",
    "> 3.字符串的拼接：+  \n",
    "> 4.in 检查成员是否存在\n",
    "\n",
    "* 字符串的一般方法（function/Methods 函数/方法）\n",
    "> 1.isdigit()\n",
    "> 2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f033f5b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"广州南方学院团委\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "85f8c935",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7d976922",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'州'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7c476a38",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'委'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1f6fafd5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'团'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "926f0d79",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "string index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [6]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mtext\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m100\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[1;31mIndexError\u001b[0m: string index out of range"
     ]
    }
   ],
   "source": [
    "text[100]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c460082c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f4458841",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0] + text[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4ad8d160",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0:6]    # 等价于  text[:6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a37e2394",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-1:-3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9f59ebee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'院团'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-3:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0039f5f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'团委'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "782dbd67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院团委广州南方学院团委广州南方学院团委'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3*text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88086573",
   "metadata": {},
   "outputs": [],
   "source": [
    "* in 检查成员对象\n",
    "* count()检查字符串出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "992f2976",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"广州\" in text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7efa3157",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"北京\" in text "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1cc8134d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3*text.count(\"广州\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f76d8dbc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'团委'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[6:8]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a82a727",
   "metadata": {},
   "source": [
    "* isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "aa0ba9d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_1 = 19\n",
    "age_2 = \"19\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "9084ca6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19 <class 'int'>\n",
      "19 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(age_1,type(age_1))\n",
    "print(age_2,type(age_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "e63f9104",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "38\n"
     ]
    }
   ],
   "source": [
    "# 如果字符串是数值，可以用int转换成真正的数值\n",
    "if age_2.isdigit():\n",
    "    print(age_1 + int(age_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "bf0b7ba3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "38"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_1 + int(age_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80f7c679",
   "metadata": {},
   "source": [
    "## 2.有序的数据结构_列表\n",
    "\n",
    "* 1.列表的概念和基本性质  \n",
    "\n",
    "> A.Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.\n",
    "\n",
    "* 2.列表一般方法(如何学习）\n",
    "\n",
    "> A.使用》Tab键查看列表的方法\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73dedb2e",
   "metadata": {},
   "source": [
    "### 2.1列表的性质\n",
    "\n",
    "* 和字符串的运算法则完全一致\n",
    "\n",
    "* 注意：字符串是对字符串进行操作，列表是对items进行操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "427947c0",
   "metadata": {},
   "source": [
    "# 列表的形式\n",
    "squares = [1,4,9,16,25]   # 列表里面放数值\n",
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "68b06027",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "72c7e627",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4 in squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "02542b55",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares.count(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "dd1bcf74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jan']"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Jack\",\"Alex\",\"Marry\",\"Jan\"]  # 列表里面放字符串\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "103b82a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 18, '李华', 20]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info = [\"Jack\",18,\"李华\",20]\n",
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "3549744a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 'Jack', 'Alex', 'Marry', 'Jan']"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares + names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "44d435dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2*squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "9544f2e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Alex', 'Marry']"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "062b3874",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Jan']"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "5b577001",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Marry']"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出“Jack”和“Marry”用切片的方法\n",
    "names[:3:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42d984fd",
   "metadata": {},
   "source": [
    "* 列表一般方法\n",
    "\n",
    "> 1. 增加\n",
    "\n",
    "> 2.删除\n",
    "\n",
    "> 3. 改\n",
    "\n",
    "> 4. 查：in\n",
    "\n",
    "* 列表的学习方法（步骤）\n",
    "\n",
    "> 1.使用tab键查看列表的方法\n",
    "\n",
    "> 2. 使用help（查看方法的使用规则）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "b3b8b17a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jan']"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Jack\",\"Alex\",\"Marry\",\"Jan\"] \n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "570e4800",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "02c64cd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"LiHua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "b61dc91b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jan', 'LiHua']"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3be5ee1c",
   "metadata": {},
   "source": [
    "# 作业\n",
    "* 列表的方法，尝试所有的方法\n",
    "* 总结列表的方法学习心得（表格形式）\n",
    "|  列表的方法    |    Description    |   使用方法    |    备注    |\n",
    "|-----------------|---------------------|------------------|--------------|\n",
    "|append        |   Append object to the end of the list.|list.append(object)|注意：增加在末尾|\n",
    "paragraph      |   Text         |   Text        |   Text     | "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b28fc53c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
